package great.project.interceptor;

import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component("customTwoInterceptor")
public class TwoInterceptor implements HandlerInterceptor {

    private static final String INTERCEPTOR_NAME = "TWO_INTERCEPTOR_";

    private static final String PRE = "PRE_HANDLE";

    private static final String POST = "POST_HANDLE";

    private static final String AFTER = "AFTER_COMPLETION";

    private static final String CUSTOM_TOKEN = "Token";


    @Override
    public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler) throws Exception {
        System.out.println(INTERCEPTOR_NAME + PRE);

        final String customToken = request.getHeader(CUSTOM_TOKEN);

        System.out.println("自定义Token =======>" + customToken);

        return !StringUtils.isEmpty(customToken);
    }

    @Override
    public void postHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println(INTERCEPTOR_NAME + POST);

    }

    @Override
    public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) throws Exception {
        System.out.println(INTERCEPTOR_NAME + AFTER);

    }
}
